import 'joke.dart';

typedef RefreshJokeCallback = void Function();

/// UI 展示随机笑话所需的状态。
class RandomJokeState {
  const RandomJokeState._({
    required this.joke,
    required this.error,
    required this.isLoading,
  });

  const RandomJokeState.loading()
    : this._(joke: null, error: null, isLoading: true);

  RandomJokeState.loadingWithPrevious(Joke? previous)
    : this._(joke: previous, error: null, isLoading: true);

  RandomJokeState.success(Joke joke)
    : this._(joke: joke, error: null, isLoading: false);

  RandomJokeState.error(Object error, {Joke? previous})
    : this._(joke: previous, error: error, isLoading: false);

  final Joke? joke;
  final Object? error;
  final bool isLoading;

  bool get hasError => error != null;
  bool get hasJoke => joke != null;
  bool get isRefreshing => isLoading && joke != null;
}
